package com.frcdi.qms.web.controller.masterdata;

import com.frcdi.qms.common.core.controller.BaseController;
import com.frcdi.qms.common.core.domain.AjaxResult;
import com.frcdi.qms.common.core.page.TableDataInfo;
import com.frcdi.qms.common.utils.poi.ExcelUtil;
import com.frcdi.qms.masterdata.domain.Unit;
import com.frcdi.qms.masterdata.service.IUnitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 单位Controller
 *
 * @author cell
 * @date 2023-09-25
 */
@Api(tags = "单位")
@RestController
@RequestMapping("/masterData/unit")
public class UnitController extends BaseController
{
    @Autowired
    private IUnitService unitService;

    /**
     * 查询单位列表
     */
    @ApiOperation("查询单位列表")
    @PreAuthorize("@ss.hasPermi('masterData:unit:list')")
    @GetMapping("/list")
    public TableDataInfo list(Unit unit)
    {
        startPage();
        List<Unit> list = unitService.selectUnitList(unit);
        return getDataTable(list);
    }

    /**
     * 导出单位列表
     */
    @ApiOperation("导出单位列表")
    @PreAuthorize("@ss.hasPermi('masterData:unit:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Unit unit)
    {
        List<Unit> list = unitService.selectUnitList(unit);
        ExcelUtil<Unit> util = new ExcelUtil<Unit>(Unit.class);
        util.exportExcel(response, list, "单位数据");
    }

    /**
     * 获取单位详细信息
     */
    @ApiOperation("获取单位详细信息")
    @PreAuthorize("@ss.hasPermi('masterData:unit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(unitService.selectUnitById(id));
    }

    /**
     * 新增单位
     */
    @ApiOperation("新增单位")
    @PreAuthorize("@ss.hasPermi('masterData:unit:add')")
    @PostMapping
    public AjaxResult add(@RequestBody Unit unit)
    {
        if (!unitService.checkUnitNameUnique(unit))
        {
            return error("新增单位'" + unit.getUnitName() + "'失败，名称已存在");
        }
        unit.setCreateBy(getUsername());
        return toAjax(unitService.insertUnit(unit));
    }

    /**
     * 修改单位
     */
    @ApiOperation("修改单位")
    @PreAuthorize("@ss.hasPermi('masterData:unit:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody Unit unit)
    {
        if (!unitService.checkUnitNameUnique(unit))
        {
            return error("修改单位'" + unit.getUnitName() + "'失败，名称已存在");
        }
        unit.setUpdateBy(getUsername());
        return toAjax(unitService.updateUnit(unit));
    }

    /**
     * 删除单位
     */
    @ApiOperation("删除单位")
    @PreAuthorize("@ss.hasPermi('masterData:unit:remove')")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(unitService.deleteUnitByIds(ids));
    }
}
